var express = require('express');
var router = express.Router();

let leveService = require("../service/leveinfoService");

let jwt = require("jsonwebtoken");

/* GET cheng page. */
router.get('/', function (req, res, next) {
    res.render('index', { title: 'Express' });
});

router.get('/list', (req, res, next) => {
    res.json({
        code: 200,
        mas: '好好好'
    })
});

// 新增请假列表
router.post('/add', (req, res, next) => {

    let { reason, start, end, extend, type = 1 } = req.body;

    let token = req.headers.token

    jwt.verify(token, "abc", async (err, info) => {
        if (err) {
            res.json({
                code: "201",
                msg: "token解析异常",
            });
        } else {
            if (info.usertype === 2) {
                let result = await leveService.add([{
                    userid: info.userid,
                    username: info.username,
                    reason,
                    start,
                    end,
                    extend,
                    type
                }]);

                res.json({
                    code: 200,
                    msg: "成功",
                    data: result,
                });
            } else {

                res.json({
                    code: 201,
                    msg: "你不配",
                });
            }

        }

    })


});

// 查看个人请假列表
router.get('/lookList', (req, res, next) => {

    let token = req.headers.token

    jwt.verify(token, "abc", async (err, info) => {
        if (err) {
            res.json({
                code: "201",
                msg: "token解析异常",
            });
        } else {

            if (info.usertype == 2) {
                let userid = info.userid;
                let result = await leveService.query({
                    userid
                });
                res.json({
                    code: '200',
                    result
                })
            }
        }

    })

});

// 查看全部请假列表
router.get('/lookAllList', (req, res, next) => {

    let token = req.headers.token

    jwt.verify(token, "abc", async (err, info) => {
        if (err) {
            res.json({
                code: "201",
                msg: "token解析异常",
            });
        } else {

            if (info.usertype == 1) {
                let result = await leveService.queryAll();
                res.json({
                    code: '200',
                    result
                })
            }
        }

    })

});

// 修改审批状态
router.patch('/PatchList', (req, res, next) => {

    let { id, type } = req.body;

    let token = req.headers.token

    jwt.verify(token, "abc", async (err, info) => {
        if (err) {
            res.json({
                code: "201",
                msg: "token解析异常",
            });
        } else {
            if (info.usertype == 1) {
                let result = await leveService.change({ _id: id }, { $set: { type } });
                if (result.acknowledged) {
                    res.json({
                        code: "200",
                        msg: '修改成功'
                    });
                } else {
                    res.json({
                        code: "201",
                        msg: '修改失败'
                    });
                }

            }
        }

    })


})


module.exports = router;
